package com.crawler.harbour.utils;

import java.lang.reflect.Method;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 * @author Administrator
 *
 */
public class EncryptUtil {
	private static EncryptUtil instance = null;

	public static EncryptUtil getInstance() {
		if (instance == null) {
			synchronized (EncryptUtil.class) {
				if (instance == null) {
					instance = new EncryptUtil();
				}
			}
		}
		return instance;
	}

	/**
	 * 加密网站参数 适用于 www.xhct.com.cn/www.xhdct.com:8081/
	 * 
	 * @param message
	 *            要加密的数据
	 * @param key
	 *            加密用的key
	 * @return 加密后的结果
	 */
	public String encrptWebParam(String message, String key) {
		if (message == null) {
			throw new IllegalArgumentException("加密内容不能为null");
		}
		if (key == null) {
			throw new IllegalArgumentException("加密的密钥不能为null");
		}

		String encrptResult = null;
		String md5Key = MD5Util.getMD5(key).substring(0, 16);
		try {
			byte[] aesMessage = paramAesEncrypt(message, md5Key);
			encrptResult = Base64Util.encode(aesMessage);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return encrptResult;
	}

	public static void main(String[] args) {
		String message = "*";
		String key = "2uiq3mkdkyoyq4irv22ss1k1";
		String result = EncryptUtil.getInstance().encrptWebParam(message, key);
		System.out.println(result);
	}

	/**
	 * 对app端密码进行加密
	 * 
	 * @param paramString
	 * @return
	 */
	public String encryptAppPwd(String paramString) {
		String str1 = "";
		try {
			str1 = DES.encryptDES(paramString, "EPORTAPP");
			str1 = str1.replace("=", "DENGHAO");
			String str3 = str1.replace("+", "JIAHAO");
			return str3;
		} catch (Exception localException) {
			String str2 = str1;
			localException.printStackTrace();
			return str2;
		}
	}

	/**
	 * @param sSrc
	 *            加密字符串
	 * @param sKey
	 *            aes加密key
	 * @return 加密后的参数
	 * @throws NoSuchPaddingException
	 * @throws NoSuchAlgorithmException
	 * @throws InvalidAlgorithmParameterException
	 * @throws InvalidKeyException
	 * @throws BadPaddingException
	 * @throws IllegalBlockSizeException
	 */
	public byte[] paramAesEncrypt(String sSrc, String sKey) throws NoSuchAlgorithmException, NoSuchPaddingException,
			InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
		if (sKey == null) {
			System.out.print("Key为空null");
			return null;
		}
		// 判断Key是否为16位
		if (sKey.length() != 16) {
			System.out.print("Key长度不是16位");
			return null;
		}
		byte[] raw = sKey.getBytes();
		SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
		Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");// "算法/模式/补码方式"
		IvParameterSpec iv = new IvParameterSpec(sKey.getBytes());// 使用CBC模式，需要一个向量iv，可增加加密算法的强度
		cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
		return cipher.doFinal(sSrc.getBytes());
	}
	/**
	 * 
	 * 将AES加密过的密文解密
	 * @param sSrc
	 *            加密过的字符串
	 * @param sKey
	 *            AES加密key
	 * @return 解密后的数据
	 * @throws NoSuchPaddingException
	 * @throws NoSuchAlgorithmException
	 * @throws InvalidAlgorithmParameterException
	 * @throws InvalidKeyException
	 * @throws BadPaddingException
	 * @throws IllegalBlockSizeException
	 */
	public byte[] paramAesDecrypt(byte[] sSrc, String sKey) throws NoSuchAlgorithmException, NoSuchPaddingException,
			InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
		if (sKey == null) {
			System.out.print("Key为空null");
			return null;
		}
		// 判断Key是否为16位
		if (sKey.length() != 16) {
			System.out.print("Key长度不是16位");
			return null;
		}
		byte[] raw = sKey.getBytes();
		SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
		Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");// "算法/模式/补码方式"
		IvParameterSpec iv = new IvParameterSpec(sKey.getBytes());// 使用CBC模式，需要一个向量iv，可增加加密算法的强度
		cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
		return cipher.doFinal(sSrc);
	}
	
	/**
	 * 解密web参数密文
	 * @param message  
	 * @param key 
	 * @return 解密后的数据
	 */
	public String decrptWebParam(String message, String key) {
		if (message == null) {
			throw new IllegalArgumentException("加密内容不能为null");
		}
		if (key == null) {
			throw new IllegalArgumentException("加密的密钥不能为null");
		}

		String decrptResult = null;
		String md5Key = MD5Util.getMD5(key).substring(0, 16);
		try {
			byte[] msg =Base64Util.decode(message);
			byte[] aesMessage = paramAesDecrypt(msg, md5Key);
			decrptResult=new String(aesMessage);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return decrptResult;
	}

}
